﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmDm.
	/// </summary>
	public class frmTheodoihd : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butKetthuc;
		private AccessData a;
		private DataTable dt=new DataTable();
		private DataTable dtct=new DataTable();
		private System.Windows.Forms.Button butMoi;
		public long id=0;
		public string sohd="",mmyy=""; 
		public decimal conlai=0;
		private string user,tendt,sql;
		private long madt;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.Button butInct;
		private System.Windows.Forms.Button butTonghop;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmTheodoihd(AccessData acc,long _madt,string _tendt)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;madt=_madt;tendt=_tendt;
			this.Text=" Công nợ :"+_tendt;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmTheodoihd));
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.butMoi = new System.Windows.Forms.Button();
			this.dataGrid2 = new System.Windows.Forms.DataGrid();
			this.chkAll = new System.Windows.Forms.CheckBox();
			this.butInct = new System.Windows.Forms.Button();
			this.butTonghop = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
			this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
			this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid1.Location = new System.Drawing.Point(8, -13);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.RowHeaderWidth = 10;
			this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
			this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.Size = new System.Drawing.Size(728, 357);
			this.dataGrid1.TabIndex = 19;
			this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butKetthuc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(433, 488);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(68, 25);
			this.butKetthuc.TabIndex = 10;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// butMoi
			// 
			this.butMoi.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butMoi.Image = ((System.Drawing.Bitmap)(resources.GetObject("butMoi.Image")));
			this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butMoi.Location = new System.Drawing.Point(307, 488);
			this.butMoi.Name = "butMoi";
			this.butMoi.Size = new System.Drawing.Size(60, 25);
			this.butMoi.TabIndex = 7;
			this.butMoi.Text = "    &Chọn";
			this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
			// 
			// dataGrid2
			// 
			this.dataGrid2.AlternatingBackColor = System.Drawing.Color.Lavender;
			this.dataGrid2.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid2.BackColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid2.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid2.CaptionForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.DataMember = "";
			this.dataGrid2.FlatMode = true;
			this.dataGrid2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid2.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.GridLineColor = System.Drawing.Color.Gainsboro;
			this.dataGrid2.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
			this.dataGrid2.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid2.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid2.Location = new System.Drawing.Point(8, 336);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
			this.dataGrid2.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid2.ReadOnly = true;
			this.dataGrid2.RowHeaderWidth = 10;
			this.dataGrid2.SelectionBackColor = System.Drawing.Color.CadetBlue;
			this.dataGrid2.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid2.Size = new System.Drawing.Size(728, 144);
			this.dataGrid2.TabIndex = 20;
			// 
			// chkAll
			// 
			this.chkAll.Location = new System.Drawing.Point(8, 488);
			this.chkAll.Name = "chkAll";
			this.chkAll.Size = new System.Drawing.Size(104, 16);
			this.chkAll.TabIndex = 21;
			this.chkAll.Text = "Xem tất cả";
			// 
			// butInct
			// 
			this.butInct.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butInct.Image = ((System.Drawing.Bitmap)(resources.GetObject("butInct.Image")));
			this.butInct.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butInct.Location = new System.Drawing.Point(370, 488);
			this.butInct.Name = "butInct";
			this.butInct.Size = new System.Drawing.Size(60, 25);
			this.butInct.TabIndex = 43;
			this.butInct.Text = "    In";
			this.butInct.Click += new System.EventHandler(this.butInct_Click);
			// 
			// butTonghop
			// 
			this.butTonghop.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butTonghop.Image = ((System.Drawing.Bitmap)(resources.GetObject("butTonghop.Image")));
			this.butTonghop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butTonghop.Location = new System.Drawing.Point(244, 488);
			this.butTonghop.Name = "butTonghop";
			this.butTonghop.Size = new System.Drawing.Size(60, 25);
			this.butTonghop.TabIndex = 44;
			this.butTonghop.Text = "    &Xem";
			this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
			// 
			// frmTheodoihd
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.butKetthuc;
			this.ClientSize = new System.Drawing.Size(744, 549);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butTonghop,
																		  this.butInct,
																		  this.chkAll,
																		  this.dataGrid2,
																		  this.butMoi,
																		  this.butKetthuc,
																		  this.dataGrid1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "frmTheodoihd";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Công nợ khách hàng";
			this.Load += new System.EventHandler(this.frmTheodoihd_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmTheodoihd_Load(object sender, System.EventArgs e)
		{
			user=a.user;
			load_grid();
			AddGridTableStyle();
			AddGridTableStyle1();
		}

		private void load_grid()
		{
			sql="select id,sohd,to_char(ngayhd,'dd/mm/yyyy') as ngayhd,noidung,madt,stien,stdatra,stien-stdatra as conlai,to_char(ngayhd,'yymmdd') as yymmdd,nvl(mmyy,to_char(ngay,'mmyy')) as mmyy from "+user+".a_theodoihd ";
			sql+=" where madt="+madt;
			if (!chkAll.Checked) sql+="  and stien-stdatra>0";
			sql+=" order by yymmdd,sohd";
			dt=a.get_data(sql).Tables[0];
			dataGrid1.DataSource=dt;
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
			id=0;sohd="";
			if (dt.Rows.Count>0)
			{
				id=long.Parse(dt.Rows[0]["id"].ToString());
				sohd=dt.Rows[0]["sohd"].ToString();
				conlai=decimal.Parse(dt.Rows[0]["conlai"].ToString());
				sohd+="{"+conlai.ToString("###,###,###,###,###")+"}";
				mmyy=dt.Rows[0]["mmyy"].ToString();
			}
			load_gridct();
		}

		private void load_gridct()
		{
			sql="select id,stt,so,to_char(ngay,'dd/mm/yyyy') as ngay,no,co,sotien,ghichu from "+user+".a_theodoihdct where id="+id+" order by stt";
			dtct=a.get_data(sql).Tables[0];
			dataGrid2.DataSource=dtct;
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid2.DataSource,dataGrid2.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}
		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dt.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();			
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sohd";
			TextCol.HeaderText = "Hóa đơn";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ngayhd";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "noidung";
			TextCol.HeaderText = "Nội dung";
			TextCol.Width = dataGrid1.Width-(20+20+100+80+100+100+100);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format="### ### ### ### ###";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stdatra";
			TextCol.HeaderText = "Đã thanh toán";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format="### ### ### ### ###";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "conlai";
			TextCol.HeaderText = "Còn lại";
			TextCol.Width = 100;
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format="### ### ### ### ###";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "mmyy";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}


		private void AddGridTableStyle1()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dtct.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stt";
			TextCol.HeaderText = "Đợt";
			TextCol.Width = 30;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "so";
			TextCol.HeaderText = "Số CT";
			TextCol.Width = 60;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ngay";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "no";
			TextCol.HeaderText = "TK Nợ";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "co";
			TextCol.HeaderText = "TK Có";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Alignment=HorizontalAlignment.Right;
			TextCol.Format="### ### ### ### ###";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ghichu";
			TextCol.HeaderText = "Ghi chú";
			TextCol.Width = dataGrid2.Width-(20+10+30+60+80+80+80+80);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);			
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			id=0;sohd="";conlai=0;
			a.close();System.GC.Collect();this.Close();
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{			
			a.close();System.GC.Collect();this.Close();
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				id=long.Parse(dataGrid1[i,0].ToString());
				sohd=dataGrid1[i,1].ToString();
				dataGrid2.CaptionText=" Số hóa đơn :"+sohd+", ngày :"+dataGrid1[i,2].ToString().Trim();
				conlai=decimal.Parse(dataGrid1[i,6].ToString());
				sohd+="{"+conlai.ToString("###,###,###,###,###")+"}";
				mmyy=dataGrid1[i,7].ToString();
				load_gridct();
			}
			catch{id=0;mmyy=sohd="";}
		}

		private void butInct_Click(object sender, System.EventArgs e)
		{
			if (dtct.Rows.Count==0) return;
			sql="select a.id,a.soqd as sophieu,to_char(a.ngayqd,'dd/mm/yyyy') ngaysp,a.sohd,to_char(a.ngayhd,'dd/mm/yyyy') ngayhd,";
			sql+="'' as madv,b.no,b.co,a.stien as sotien,a.stdatra as datra,b.ghichu,b.so,to_char(b.ngay,'dd/mm/yyyy') as ngay,b.sotien as sotiendot";
			sql+=" from "+user+".a_theodoihd a,"+user+".a_theodoihdct b where a.id=b.id and a.id="+id+"  order by b.stt";
			DataSet tmp=a.get_data(sql);
			foreach(DataRow r in tmp.Tables[0].Rows) r["madv"]=tendt;
			frmReport f=new frmReport(a,tmp,"","KT_a_congno_dot.rpt");
			f.ShowDialog(this);		
		}

		private void butTonghop_Click(object sender, System.EventArgs e)
		{
			load_grid();
		}
	}
}
